package com.good.child.ui.adapter.ai;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.good.child.R;
import com.good.child.bean.ai.CommunityBean;
import com.good.child.ui.utils.DateUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class AiCommunityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<CommunityBean> dataList;
    private onItemClick onItemClick;
    public static final int ITEM_TYPE_0 = 0;
    public static final int ITEM_TYPE_1 = 1;
    public static final int ITEM_TYPE_2 = 2;
    public static final int ITEM_TYPE_3 = 3;
    public static final int ITEM_TYPE_4 = 4;

    public AiCommunityAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<CommunityBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_0) {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_community_default, parent, false);
            AiCommunityAdapter.ViewHolder myViewHolder = new AiCommunityAdapter.ViewHolder(inflater);
            return myViewHolder;
        } else if (viewType == ITEM_TYPE_1) {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_community1, parent, false);
            AiCommunityAdapter.ViewHolder1 myViewHolder = new AiCommunityAdapter.ViewHolder1(inflater);
            return myViewHolder;
        } else if (viewType == ITEM_TYPE_2) {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_community2, parent, false);
            AiCommunityAdapter.ViewHolder2 myViewHolder = new AiCommunityAdapter.ViewHolder2(inflater);
            return myViewHolder;
        } else if (viewType == ITEM_TYPE_3) {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_community3, parent, false);
            AiCommunityAdapter.ViewHolder3 myViewHolder = new AiCommunityAdapter.ViewHolder3(inflater);
            return myViewHolder;
        } else {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_community4, parent, false);
            AiCommunityAdapter.ViewHolder4 myViewHolder = new AiCommunityAdapter.ViewHolder4(inflater);
            return myViewHolder;
        }
    }

    @Override
    public int getItemViewType(int position) {
        CommunityBean bean = dataList.get(position);
        return bean.getType();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }
        CommunityBean bean = dataList.get(position);
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.tv_name.setText(bean.getUserName());
            viewHolder.tv_content.setText(bean.getContent());
            if (!TextUtils.isEmpty(bean.getImageUrl())) {
                viewHolder.fl_images.setVisibility(View.VISIBLE);
                if (bean.getImageUrl().contains(";")) {
                    String[] split = bean.getImageUrl().split(";");
                    if (split == null || split.length == 0) {
                        return;
                    }
                    for (int i = 0; i< split.length;i++) {
                        if (i == 0) {
                            Glide.with(context).load(split[0]).placeholder(context.getResources().getDrawable(R.mipmap.default_head)).into(viewHolder.riv_image1);
                            viewHolder.riv_image1.setVisibility(View.VISIBLE);
                        }else if (i == 1) {
                            Glide.with(context).load(split[1]).placeholder(context.getResources().getDrawable(R.mipmap.default_head)).into(viewHolder.riv_image2);
                            viewHolder.riv_image2.setVisibility(View.VISIBLE);
                        }else if (i == 2) {
                            Glide.with(context).load(split[2]).placeholder(context.getResources().getDrawable(R.mipmap.default_head)).into(viewHolder.riv_image3);
                            viewHolder.riv_image3.setVisibility(View.VISIBLE);
                        }else if (i == 3) {
                            Glide.with(context).load(split[3]).placeholder(context.getResources().getDrawable(R.mipmap.default_head)).into(viewHolder.riv_image4);
                            viewHolder.riv_image4.setVisibility(View.VISIBLE);
                        }
                    }
                } else {
                    Glide.with(context).load(bean.getImageUrl()).into(viewHolder.riv_image1);
                }
                viewHolder.riv_image1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClick != null) {
                            onItemClick.onClickView(bean);
                        }
                    }
                });
                viewHolder.riv_image2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClick != null) {
                            onItemClick.onClickView(bean);
                        }
                    }
                });
                viewHolder.riv_image3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClick != null) {
                            onItemClick.onClickView(bean);
                        }
                    }
                });
                viewHolder.riv_image4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClick != null) {
                            onItemClick.onClickView(bean);
                        }
                    }
                });
                viewHolder.fl_images.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClick != null) {
                            onItemClick.onClickView(bean);
                        }
                    }
                });

            } else {
                viewHolder.fl_images.setVisibility(View.GONE);
            }
            if (DateUtil.isToday(bean.getCreateTime(), "yyyy-MM-dd HH:mm:ss")) {
                viewHolder.tv_date.setCompoundDrawables(context.getDrawable(R.mipmap.icon_today), null, null, null);
                viewHolder.tv_date.setText("今天");
            } else {
                viewHolder.tv_date.setCompoundDrawables(null, null, null, null);
                viewHolder.tv_date.setText(DateUtil.formatTimeAgo(bean.getCreateTime()));
            }
            Glide.with(context).load(R.mipmap.default_head).into(viewHolder.im_head);
        } else if (holder instanceof ViewHolder1) {
            ViewHolder1 viewHolder = (ViewHolder1) holder;
            if (!TextUtils.isEmpty(bean.getImageUrl())) {
                Uri uri = Uri.parse(bean.getImageUrl());
                Glide.with(context).load(uri).into(viewHolder.im_icon);
            }
            if (!TextUtils.isEmpty(bean.getContent())) {
                viewHolder.tv_content.setText(bean.getContent());
            }

        } else if (holder instanceof ViewHolder2) {
            ViewHolder2 viewHolder = (ViewHolder2) holder;
        } else if (holder instanceof ViewHolder3) {
            ViewHolder3 viewHolder = (ViewHolder3) holder;
        } else {
            ViewHolder4 viewHolder = (ViewHolder4) holder;
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 1 : dataList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.im_head)
        ImageView im_head;
        @BindView(R.id.fl_images)
        FrameLayout fl_images;
        @BindView(R.id.riv_image4)
        ImageView riv_image4;
        @BindView(R.id.riv_image3)
        ImageView riv_image3;
        @BindView(R.id.riv_image2)
        ImageView riv_image2;
        @BindView(R.id.riv_image1)
        ImageView riv_image1;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.im_icon)
        ImageView im_icon;

        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {


        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder3 extends RecyclerView.ViewHolder {


        public ViewHolder3(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder4 extends RecyclerView.ViewHolder {

        public ViewHolder4(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onItemClick {
        void onClickView(CommunityBean bean);
    }
}
